<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/12/30
 * Time: 15:39
 */
namespace app\wxapi\controller;

use think\Db;
use think\Request;
use think\Controller;
use app\common\model\LogOrderModel as lo;
use app\common\model\OrderCancelModel as ocm;
use think\Session;
class Cancel extends Nocommon
{
    /*
     * 景点核销码
     */
    public function tour_cancel_code()
    {
        if(Request::instance()->isPost()){
            $post = Request::instance()->post();
            $res = Db::name('tour_order')->where('cancel_code',$post['cancel_code'])->find();
            //处理今天的时间戳
            $beginToday=mktime(0,0,0,date('m'),date('d'),date('Y'));
            $endToday=mktime(0,0,0,date('m'),date('d')+1,date('Y'))-1;
            if($res){
                if($res['tour_id'] != $post['id'] || $post['code'] !=1){
                    $this->apiReturn('401','非本店订单');
                }
                $res = Db::name('tour_order')->alias('to')
                    ->join('tours t','to.tour_id=t.tour_id','LEFT')
                    ->join('tour_ticket tt','tt.tid=to.type_id','LEFT')
                    ->field('to.order_id,to.order_sn,to.order_price,to.linkman,to.tel,to.ticket_date,to.order_status,to.ticket_count,t.tour_name,to.create_time,tt.tname')
                    ->where('to.order_id',$res['order_id'])
                    ->find();
                //判断是否是今天的
                if($res['ticket_date']<$beginToday || $res['ticket_date']>$endToday){
                    $this->apiReturn('400','非本日门票订单');
                }
                //处理时间
                $res['ticket_date'] = date('Y-m-d',$res['ticket_date']);
                $res['create_time'] = date('Y-m-d H:i:s',$res['create_time']);
                if($res['order_status']==1){
                    $res['is_cancel'] = 1;
                }else if($res['order_status']==0 || $res['order_status']==2 || $res['order_status']==5){
                    $this->apiReturn('400','核销码错误');
                }else if($res['order_status']==7){
                    $this->apiReturn('400','订单已过期');
                }
                else{
                    $res['is_cancel'] = 0;
                }
                unset($res['order_status']);
                $this->apiReturn('200','成功',$res);
            }else{
                $this->apiReturn('400','核销码错误');
            }
        }
    }

    /**
     * 酒店核销码
     */
    public function hotel_cancel_code()
    {
        if(Request::instance()->isPost()){
            $post = Request::instance()->post();
            $res = Db::name('hotel_order')->where('cancel_code',$post['cancel_code'])->find();
            //处理今天的时间戳
            $beginToday=mktime(0,0,0,date('m'),date('d'),date('Y'));
            $endToday=mktime(0,0,0,date('m'),date('d')+1,date('Y'))-1;
            if($res){
                if($res['hotel_id'] != $post['id'] || $post['code'] !=2){
                    $this->apiReturn('401','非本店订单');
                }
                $res =  Db::name('hotel_order')->alias('ho')
                    ->join('hotel h','ho.hotel_id=h.hotel_id','LEFT')
                    ->join('hotel_room r','ho.room_id=r.room_id','left')
                    ->field('ho.order_id,ho.order_price,ho.order_sn,ho.room_name,ho.linkman,ho.tel,ho.room_amount,ho.create_time,ho.order_price,ho.order_status,ho.start_time,ho.end_time,h.hotel_name')
                    ->where('ho.order_id',$res['order_id'])
                    ->find();
                //判断是否是今天的
                if($res['start_time']<$beginToday || $res['start_time']>$endToday){
                    $this->apiReturn('400','非本日酒店订单');
                }
                $res['start_time'] = date('Y-m-d',$res['start_time']);
                $res['end_time'] = date('Y-m-d',$res['end_time']);
                $res['create_time'] = date('Y-m-d H:i:s',$res['create_time']);
                if($res['order_status']==1){
                    $res['is_cancel'] = 1;
                }else if($res['order_status']==0 || $res['order_status']==2 || $res['order_status']==5 || $res['order_status']==6){
                    $this->apiReturn('400','核销码错误');

                }else if($res['order_status']==7){
                    $this->apiReturn('400','订单已过期');
                }
                else{
                    $res['is_cancel'] = 0;
                }
                unset($res['order_status']);
                $this->apiReturn('200','成功',$res);
            }else{
                $this->apiReturn('400','核销码错误');
            }

        }
    }

    /*
     * 旅行社核销接口
     */
    public function team_cancel_code()
    {
        if(Request::instance()->isPost()){
            $post = Request::instance()->post();
            $res = Db::name('gtour_order')->where('cancel_code',$post['cancel_code'])->find();
            //处理今天的时间戳
            $beginToday=mktime(0,0,0,date('m'),date('d'),date('Y'));
            $endToday=mktime(0,0,0,date('m'),date('d')+1,date('Y'))-1;
            if($res){
                if($res['team_id'] != $post['id'] || $post['code'] !=3){
                    $this->apiReturn('401','非本店订单');
                }
                $res = Db::name('gtour_order')->alias('go')
                    ->join('group_tour gt','go.gtour_id=gt.id','LEFT')
                    ->join('team t','t.team_id=gt.team_id','left')
                    ->field('go.order_id,go.order_price,go.order_sn,go.gtour_number,go.linkman,go.tel,go.gtour_start,go.order_price,go.order_status,go.create_time,gt.gtour_title,t.team_name')
                    ->where('go.order_id',$res['order_id'])
                    ->find();
                if($res['gtour_start']<$beginToday || $res['gtour_start']>$endToday){
                    $this->apiReturn('400','非本日旅行社订单');
                }
                $res['gtour_start'] = date('Y-m-d',$res['gtour_start']);
                $res['create_time'] = date('Y-m-d H:i:s',$res['create_time']);
                if($res['order_status']==1){
                    $res['is_cancel'] = 1;
                }else if($res['order_status']==0 || $res['order_status']==2 || $res['order_status']==5){
                    $this->apiReturn('400','核销码错误');
                }else if($res['order_status']==7){
                    $this->apiReturn('400','订单已过期');
                }
                else{
                    $res['is_cancel'] = 0;
                }
                unset($res['order_status']);
                $this->apiReturn('200','成功',$res);
            }else{
                $this->apiReturn('402','核销码错误');
            }
        }
    }

    /*
     * 核销确认接口
     */
    public function cancel_submit()
    {
        if(Request::instance()->isPost()){
            $post = Request::instance()->post();
            //景点核销
            if($post['code']==1){
                $order = Db::name('tour_order')->alias('a')
                    ->field('a.order_id,a.order_sn,a.order_status,t.no_settled_order_fee,a.ticket_price,a.ticket_count,a.commission_rate,t.no_settled_order_num,a.order_price,a.ticket_date')
                    ->join('tours t','a.tour_id=t.tour_id','left')->where('order_id', $post['order_id'])->find();
                if($order){
                    Db::startTrans();
                    try {
                        //计算订单佣金
                        $commissionFee = 0;
                        if ($order['commission_rate'] > 0) {
                            $commissionFee = $order['ticket_price'] * $order['ticket_count'] * $order['commission_rate'] / 1000;

                        }
                        // 核销该订单信息
                        $OrderCancel = new ocm();
                        $OrderCancel->orderCancel($post['admin_id'], 1, $order['order_id'], $order['order_sn'],$order['order_price']);
                        //创建订单日志
                        $logOrder = new lo();
                        $logOrder->addLogOrder($order['order_id'], 1, $post['admin_id'], 3, '核销成功，用户已经入住');
                        //修改商家未计算订单数和未结算的订单佣金
                        $tour_id = Db::name('tour_admin')->field('tour_id')->where('t_admin_id',$post['admin_id'])->find();
                        Db::name("tours")
                            ->where("tour_id", $tour_id['tour_id'])
                            ->setInc("no_settled_order_num", 1);


                        Db::name("tours")
                            ->where("tour_id", $tour_id['tour_id'])
                            ->setDec('no_settled_order_fee',$commissionFee);

                        $data = [
                            'order_status'   => 3,
                            'commission_fee' => $commissionFee,
                        ];
                        //修改订单状态
                        $res = Db::name('tour_order')->where('order_id', $order['order_sn'])->update($data);
                        if($res){

                            Db::commit();
                            //修改订单状态
                            $this->apiReturn('200','核销成功');

                        }else{
                            Db::rollback();
                            $this->apiReturn('400','核销失败');
                        }
                    }catch (\Exection $e){
                        Db::rollback();
                        $this->apiReturn('400','核销失败');
                    }
                }else{
                    $this->apiReturn('400', '该笔订单不存在');
                }

            }
            if($post['code']==2){
                $where = ['order_sn'=>$post['order_sn'],'ho.is_del'=>0,'order_status'=>1];
                $result = Db::name('hotel_order')
                    ->alias("ho")
                    ->field('order_id,ho.uid,ho.room_name,ho.order_sn,h.no_settled_order_fee,total_price,room_amount,ho.commission_rate,no_settled_order_num,ho.order_price')
                    ->join('hotel h','h.hotel_id=ho.hotel_id','left')
                    ->where($where)
                    ->find();
                if($result){
                    Db::startTrans();
                    try{
                        //计算订单佣金
                        $commissionFee = 0;
                        if($result['commission_rate']>0){
                            $commissionFee = $result['total_price']*$result['room_amount']*$result['commission_rate']/1000;
                        }
                        // 核销该订单信息
                        $OrderCancel = new ocm();
                        $OrderCancel->orderCancel($post['admin_id'],2,$result['order_id'],$post['order_sn'],$result['order_price']);
                        //创建订单日志
                        $logOrder = new lo();
                        $logOrder->addLogOrder($result['order_id'],1, $post['admin_id'], 3, '核销成功，用户已经入住');
                        $hotel = Db::name('hotel_admin')->field('hotel_id')->where('h_admin_id',$post['admin_id'])->find();
                        Db::name("hotel")
                            ->where("hotel_id",$hotel['hotel_id'])
                            ->setInc("no_settled_order_num",1);
                        Db::name("hotel")
                            ->where("hotel_id",$hotel['hotel_id'])
                            ->setDec("no_settled_order_fee",$commissionFee);
                        $res =  Db::name('hotel_order')
                            ->alias("ho")
                            ->where($where)
                            ->update(['order_status'=>3,'commission_fee'=>$commissionFee]);
                        if($res){

                            Db::commit();
                            $this->apiReturn('200','核销成功');

                        }else{
                            Db::rollback();
                            $this->apiReturn('400','核销失败');
                        }
                    }catch (\Exection $e){
                        Db::rollback();
                        $this->apiReturn('400','核销失败');
                    }
                }else{
                    $this->apiReturn('400', '该笔订单不存在');
                }

            }
            if($post['code']==3){
                $order = Db::name('gtour_order')
                    ->alias('a')->field('a.*,t.no_settled_order_fee,t.no_settled_order_num')
                    ->join('team t','a.team_id=t.team_id','left')->where('order_id', $post['order_id'])->find();
                if ($order) {
                    Db::startTrans();

                    try {
                        //计算订单佣金
                        $commissionFee = 0;
                        if ($order['commission_rate'] > 0) {
                            $commissionFee = $order['total_price'] * $order['gtour_number'] * $order['commission_rate'] / 1000;

                        }

                        // 核销该订单信息
                        $OrderCancel = new ocm();
                        $OrderCancel->orderCancel($post['admin_id'], 1, $order['order_id'], $order['order_sn'], $order['order_price']);
                        //创建订单日志
                        $logOrder = new lo();
                        $logOrder->addLogOrder($order['order_id'], 1, $post['admin_id'], 3, "核销成功，用户已经入住");
                        //修改商家未计算订单数和未结算的订单佣金
                        $team = Db::name('team_admin')->field('team_id')->where('t_admin_id', $post['admin_id'])->find();
                        Db::name("team")
                            ->where("team_id", $team['team_id'])
                            ->setInc("no_settled_order_num", 1);

                        Db::name("team")
                            ->where("team_id", $team['team_id'])
                            ->setDec('no_settled_order_fee', $commissionFee);

                        $data = [
                            'order_status' => 3,
                            'commission_fee' => $commissionFee,
                        ];
                        //修改订单状态
                        $res = Db::name('gtour_order')->where('order_id', $postData['id'])->update($data);
                        if ($res) {
                            Db::commit();
                            $this->apiReturn('200', '核销成功');
                        } else {
                            Db::rollback();
                            $this->apiReturn('400', '核销失败');
                        }
                    } catch (\Exection $e) {
                        Db::rollback();
                        $this->apiReturn('400', '核销失败');
                    }
                }else{
                    $this->apiReturn('400', '该笔订单不存在');
                }
            }
        }
    }
}